<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/14
 * Time: 22:04
 */

namespace app\index\logic;

use app\model\Leave;
use app\model\User;
use think\facade\Request;

class LeaveLogic
{
    public function doLeave()
    {
        $param = Request::post();

        $data = [
            'leave_uid' => config('user')['id'],
            'leave_orgid' => config('user')['orgid'],
        ];

        $leaveModel = new Leave();
        if(!isset($param['type']) || empty($param['type']))   fail('请选择请假类型');
        $data['leave_type'] = intval($param['type']);

        if(!isset($param['startDay']) || empty($param['startDay']))   fail('请选择开始日期');
        if(!isset($param['startHour']) || empty($param['startHour']))   fail('请选择开始时间');
        $data['leave_start_time'] = strtotime(trim($param['startDay']).' '.trim($param['startHour']));

        if(!isset($param['endDay']) || empty($param['endDay']))   fail('请选择结束日期');
        if(!isset($param['endHour']) || empty($param['endHour']))   fail('请选择结束时间');
        $data['leave_end_time'] = strtotime(trim($param['endDay']).' '.trim($param['endHour']));

        if($leaveModel->checkIfLeave(config('user')['id'],$data['leave_start_time'],$data['leave_end_time']))    fail('该时段审批已经提交，不能重复提交');

        if(!isset($param['remark']) || empty($param['remark']))   fail('请输入请假说明');
        $data['leave_remark'] = trim($param['remark']);
        if(checkPosition(config('user')['position'],User::CLEANING_HEAD)){
            //领班发起不需要额外领班审批
            $data['leave_approval'] = 2;
            $data['leave_head_approval_time'] = time();
            $data['leave_head_approval_uid'] = config('user')['id'];
            $data['leave_head_approval_name'] = config('user')['pername'];
        }
        $leaveModel = new Leave();
        $rt = $leaveModel->addOneLeave($data);

        if(!$rt)    fail('请假失败，请稍后重试');

        return $rt;
    }

    public function getStartList()
    {
        $uid = config('user')['id'];

        $where = [
            'leave_uid' => $uid
        ];
        $leaveModel = new Leave();

        return $leaveModel->getLeaveListByCondition($where);
    }

    public function getApprovalList()
    {
        $orgid = config('user')['orgid'];
        $where = [];
        $position = config('user')['position'];
        if(checkPosition($position,User::CLEANING)){
            //保洁员不能审批
            return [];
        }elseif(checkPosition($position,User::CLEANING_HEAD)) {
            //领班 审批保洁员
            $where['position'] = 1;
            $where['leave_approval'] = 1;
        }elseif(checkPosition($position,User::CLEANING_CHARGE)) {
            //主管 审批保洁员+领班
            $where['position'] = [1,2];
            $where['leave_approval'] = 2;
        }
        $where['leave_orgid'] = $orgid;

        $leaveModel = new Leave();

        return $leaveModel->getLeaveListByCondition($where);
    }

    public function getInfo($leaveId){

        $leaveModel = new Leave();
        $info = $leaveModel->getLeaveInfoById($leaveId);

        if(!$info)  fail('请假记录不存在');

        return $info;
    }

    public function remove($leaveId){

        $leaveModel = new Leave();
        $info = $leaveModel->getLeaveInfoById($leaveId);
        if(!$info)  fail('请假记录不存在');

        //不是自己发起的不能撤回

        if($info['approval'] == 0)  fail('请假记录已经撤回，不能重复提交');
        if($info['approval'] > 2)  fail('该请假记录不能撤回');
        $where = ['leave_id'=>$leaveId];
        $update = [
            'leave_approval' => 0,
            'leave_approval_time' => time(),
            'leave_approval_uid' => config('user')['id'],
            'leave_approval_name' => config('user')['pername'],
        ];

        $rt = $leaveModel->updateLeaveByWhere($where,$update);
        if(!$rt)    fail('撤销失败');

        return true;
    }

    public function approval($leaveId,$approval,$reason = ''){

        $leaveModel = new Leave();
        $info = $leaveModel->getLeaveInfoById($leaveId);
        if(!$info)  fail('请假记录不存在');

        if($info['approval'] == Leave::APPROVAL_WITHDRAW)   fail('请假已撤回');
        if($info['approval'] == Leave::APPROVAL_REJECT)   fail('请假已拒绝');
        if($info['approval'] == Leave::APPROVAL_CHARGE)   fail('请假主管审批中');
        if($info['approval'] == Leave::APPROVAL_PASS)   fail('请假已通过');

        $where = ['leave_id'=>$leaveId];
        $update = [
            'leave_approval' => $approval,
            'leave_approval_time' => time(),
            'leave_reason' => $reason,
            'leave_approval_uid' => config('user')['id'],
            'leave_approval_name' => config('user')['pername'],
        ];

        $rt = $leaveModel->updateLeaveByWhere($where,$update);
        if(!$rt)    fail('撤销失败');

        return true;
    }

}